<?php


namespace app\admin\controller;


use app\admin\utils\Utils;
use app\model\ModelInvitationCode;
use app\model\ModelUser;
use app\model\ModelUserAccount;
use app\service\TaskService;
use easyadmin\app\columns\form\FormText;
use easyadmin\app\columns\lists\ListDateTime;
use easyadmin\app\columns\lists\ListText;
use easyadmin\app\libs\Breadcrumb;
use easyadmin\app\libs\Btn;
use easyadmin\app\libs\ListField;
use easyadmin\app\libs\PageForm;
use easyadmin\app\libs\PageList;
use easyadmin\app\libs\Pagination;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * Class User 用户管理配置类
 * @package app\admin\controller
 */
class User extends Admin
{
    protected $pageName = '用户';
    //列表设置
    protected function configList(PageList $page)
    {
        $page
            ->addAction('查看', 'show', [
                'icon' => 'layui-icon layui-icon-release',
                'class' => ['layui-btn-primary', 'layui-btn-xs'],
            ])
//            ->addAction('编辑', 'edit', [
//                'icon' => 'layui-icon layui-icon-edit',
//                'class' => ['layui-btn-primary', 'layui-btn-xs']
//            ])
            ->addAction('任务', url('userTask/lists'), [
                'icon' => 'layui-icon layui-icon-log',
                'class' => 'layui-btn-primary layui-btn-xs'
            ])
            ->addAction('账户', 'userAccount/lists', [
                'icon' => 'layui-icon layui-icon-log',
                'class' => ['layui-btn-primary', 'layui-btn-xs'],
            ]);

        $addBtn = new Btn();
        $addBtn->setLabel('添加');
        $addBtn->setUrl('add');
        $addBtn->setIcon('layui-icon layui-icon-add-1');
        $page->setActionAdd($addBtn);

        $page
//            ->setTemplate("list:list")
            ->setOrderBy(["id"=>"desc"])
        ;

    }


    //列表字段设置
    protected function configListField(ListField $list)
    {
        $list
            ->addField('id', 'ID', ListText::class)
            ->addField('vip_lv', 'VIP等级', ListText::class)
            ->addField('real_name', '用户名', ListText::class)
            ->addField('sex', '性别', ListText::class)
            ->addField('mobile', '手机号码(登录字段)', ListText::class)
//            ->addField('province', '省', ListText::class)
//            ->addField('city', '城市', ListText::class)
//            ->addField('addr', '地址', ListText::class)
            ->addField('my_invitation_code', '我的邀请码', ListText::class)
            ->addField('invitation_user_id', '邀请人id', ListText::class);
    }

    protected function configShowField(ListField $field)

    {
        $field
            ->addField('id', 'ID', ListText::class)
            ->addField('vip_lv', 'VIP等级', ListText::class)
            ->addField('real_name', '用户名', ListText::class)
            ->addField('sex', '性别', ListText::class)
            ->addField('mobile', '手机号码(登录字段)', ListText::class)
            ->addField('province', '省', ListText::class)
            ->addField('city', '城市', ListText::class)
            ->addField('addr', '地址', ListText::class)
            ->addField('my_invitation_code', '我的邀请码', ListText::class)
            ->addField('invitation_user_id', '邀请人id', ListText::class)
            ->addField('invitation_code', '邀请人邀请码', ListText::class)
            ->addField('reg_time', '注册时间', ListDateTime::class)
            ->addField('last_login_time', '最后登录时间', ListDateTime::class);
    }

    //列表添加 表单设置
    protected function configFormField(PageForm $page)
    {
        $page
            ->addField('real_name', '用户名', FormText::class, [
                'required' => true,
                'verify' => function ($val) {
                    $user = new ModelUser();
                    $res = $user->where(['real_name' => $val])->find();
                    if ($res) {
                        return '用户名重复';
                    } else {
                        return true;
                    }
                }
            ])
            ->addField('password', '密码', FormText::class, [
                'required' => true
            ])
            ->addField('sex', '性别', FormText::class)
            ->addField('mobile', '手机号码(登录字段)', FormText::class, [
                'required' => true,
                'verify' => function ($val) {
                    $user = new ModelUser();
                    $res = $user->where(['mobile' => $val])->find();
                    $chars = "/^((\(\d{2,3}\))|(\d{3}\-))?1(3|5|8|9)\d{9}$/";
                    if (!preg_match($chars, $val)) {
                        return '请输入正确的手机号';
                    } elseif ($res) {
                        return '手机号重复';
                    } else {
                        return true;
                    }
                }
            ])
            ->addField('province', '省', FormText::class)
            ->addField('city', '城市', FormText::class)
            ->addField('addr', '地址', FormText::class)
            ->addField('invitation_code', '邀请码', FormText::class);
    }

    protected function configListPagination(Pagination $pagination)
    {
        $pagination->setPageSize(20);
    }

    /**
     * 钩子函数 插入之前 操作
     * @param $data
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    protected function insertBefore($data): array
    {
        $data['pid'] = 0;
        $data['lv_path'] = '';
        $data['vip_lv'] = 0;
        $data['head_img_url'] = '';
        $data['password'] = md5($data['password']);
        $data['is_delete'] = 0;
        $data['reg_time'] = time();
        $data['last_login_time'] = time();
        $data['my_invitation_code'] = Utils::getInvitationCode();
        $data['invitation_user_id'] = '';
        $data['user_token'] = md5($data['password'] . 'mobile');
        $userModel = new ModelUser();
        if (!empty($data['invitation_code'])) {
            $user = $userModel->field('id')->where(['my_invitation_code' => $data['invitation_code']])->find();
            $data['invitation_user_id'] = $user['id'];
        }
        return $data;
    }

    /**
     * @param $data
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    protected function insertAfter($data): array
    {
        $codeModel = new ModelInvitationCode();
        $userModel = new ModelUser();
        $userAccountModel = new ModelUserAccount();
        $user = $userModel->where('my_invitation_code', '=', $data['my_invitation_code'])->find();
        //绑定邀请码
        $codeModel->insert([
            'user_id' => $user['id'],
            'my_invitation_code' => $data['my_invitation_code'],
            'invitation_code' => $data['invitation_code']
        ]);
        //绑定账户
        $userAccountModel->insert([
            'user_id' => $user['id'],
            'create_time' => time(),
            'update_time' => time()
        ]);


        return $data;
    }



}